<?php

namespace App\Admin\Controllers\user;

use App\Models\user\Feedback;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class FeedBackController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户反馈';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Feedback);

        $grid->model()->latest();
        $grid->disableExport();
        $grid->disableCreateButton();

        $grid->actions(function ($actions) {

            // 去掉删除
//            $actions->disableDelete();

            // 去掉编辑
            $actions->disableEdit();

            // 去掉查看
            $actions->disableView();
        });

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1/3, function ($filter) {

                $filter->where(function ($query) {

                    $query->whereHas('user',function ($query){
                        $query->where('uuid', 'like', "%{$this->input}%");
                    })
                        ->orWhere('id', 'like', "%{$this->input}%");

                },__('Uuid'));

            });

            $filter->column(1/3, function ($filter) {

                $filter->where(function ($query) {
                    $query->whereHas('user',function ($query){
                        $query->where('phone', 'like', "%{$this->input}%")
                            ->orWhere('name', 'like', "%{$this->input}%");
                    });

                },'昵称手机');

            });

            $filter->column(1/3, function ($filter) {
                $filter->between('created_at',__('Created at'))->datetime();
            });

        });



        $grid->column('id', __('Id'));
        $grid->column('user_id', __('User id'));
        $grid->column('user.header_img', __('header_img'))
            ->lightbox(['width' => 50, 'height' => 50, 'class' => ['circle', 'thumbnail']]);

        $grid->column('user.name', __('Name'));
        $grid->column('user.phone', __('Phone'));
        $grid->column('content', __('Content'));
        $grid->column('imgs', __('Imgs'))->lightbox(['width' => 50, 'height' => 50, 'class' => [ 'thumbnail']]);;
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Feedback::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('user_id', __('User id'));
        $show->field('content', __('Content'));
        $show->field('imgs', __('Imgs'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Feedback);

        $form->number('user_id', __('User id'));
        $form->text('content', __('Content'));
        $form->text('imgs', __('Imgs'));

        return $form;
    }
}
